
package com.ami.kvm.jviewer.kvmpkts;

import com.ami.kvm.jviewer.Debug;
import java.net.Socket;
import java.nio.ByteBuffer;

public class HeaderReader implements KVMReader {
	private KVMClient m_client;
	private byte[] m_hdrBuf;
	private ByteBuffer m_hdrByteBuf;
	private IVTPPktHdr m_pktHdr;
	private int m_hdrIx;

	public HeaderReader(KVMClient paramKVMClient) {
		this.m_client = paramKVMClient;
		this.m_hdrBuf = new byte[8];
		this.m_hdrByteBuf = ByteBuffer.wrap(this.m_hdrBuf);
		this.m_pktHdr = new IVTPPktHdr();
		this.m_hdrIx = 0;
	}

	public void initialize() {
		this.m_hdrIx = 0;
		this.m_hdrByteBuf.clear();
	}

	public int read(Socket socket) {
		try {
			byte[] data = new byte[this.m_hdrByteBuf.remaining()];
			this.m_hdrIx = this.m_client.read_data(socket, data);
			this.m_hdrByteBuf.put(data);
			if (this.m_hdrIx < 0) {
				return -1;
			}
			if (8 <= this.m_hdrIx) {
				this.m_pktHdr.set(this.m_hdrByteBuf);
				this.m_client.m_pktHdr = this.m_pktHdr;
				Object localObject = null;
				// FIXME by tianlin for frag帧数入口
				Boolean localBoolean = OnGetReader(this.m_pktHdr.type);

				if (!localBoolean.booleanValue()) {
					if (this.m_pktHdr.pktSize > 0) {
						Debug.out.println("Control\n");
						localObject = this.m_client.getCtrlReader();
						((KVMReader) localObject).initialize();
						this.m_client.setState((KVMReader) localObject);
					} else {
						Debug.out.println("Control No Body\n");
						this.m_client.onControlMessage();
						localObject = this.m_client.getHdrReader();
						((KVMReader) localObject).initialize();
						this.m_client.setState((KVMReader) localObject);
					}
				}
			}
		} catch (Exception localException) {
			Debug.out.println("HDR_RDR ");
			Debug.out.println(localException);
			return -1;
		}
		return 0;
	}

	private Boolean OnGetReader(short type) {
		Object localObject;
		// FIXME 视频流入口
		if (type == 25) {
			if (this.m_client.m_isBlank == true) {
				this.m_client.m_isBlank = false;
			}
			Debug.out.println("Video Fragment\n");
			localObject = this.m_client.getFragNumReader();
			((KVMReader) localObject).initialize();
			this.m_client.setState((KVMReader) localObject);
		} else if (type == 17) {
			Debug.out.println("BW Detect\n");
			localObject = this.m_client.getNullReader();
			((KVMReader) localObject).initialize();
			this.m_client.setState((KVMReader) localObject);
		} else {
			Debug.out.println("GET SOC READER :: Header type : " + this.m_pktHdr.type);
			localObject = this.m_client.getSocReader(this.m_pktHdr.type);
			if (localObject == null)
				return Boolean.valueOf(false);
			((KVMReader) localObject).initialize();
			this.m_client.setState((KVMReader) localObject);
		}
		return Boolean.valueOf(true);
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\kvmpkts\
 * HeaderReader.class Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */